import * as cloudDb from '@agconnect/database-server';
import { resource } from './model/resource';
import { topic } from './model/topic';
import { user_topic } from './model/user_topic';
import { user_like } from './model/user_like';
import { user_history } from './model/user_history';
import { collect } from './model/collect';
import { ListResp } from './model/ListResp';
import { topic_resource } from './model/topic_resource';
import { ResourceResp } from './model/ResourceResp';
import * as agc from '@agconnect/common-server';
import * as Utils from './utils/Utils.js';

const ZONE_NAME = "HarmonyOSDeveloper";

export class DatabaseHelper {
  logger;
  cloudDbZone;

  constructor(context, logger) {
    this.logger = logger;
    const credential = Utils.getCredential(context, logger);
    try {
      // 初始化AGCClient
      logger.info("begin to init agcClient");
      let agcClient;
      try {
        agcClient = agc.AGCClient.getInstance();
      } catch {
        agc.AGCClient.initialize(credential);
        agcClient = agc.AGCClient.getInstance();
      }
      // 初始化AGConnectCloudDB实例
      logger.info("begin to init cloudDb");
      let cloudDbInstance;
      try {
        cloudDbInstance = cloudDb.AGConnectCloudDB.getInstance(agcClient);
      } catch {
        cloudDb.AGConnectCloudDB.initialize(agcClient);
        cloudDbInstance = cloudDb.AGConnectCloudDB.getInstance(agcClient);
      }
      // 创建CloudDBZoneConfig配置对象，并设置云侧CloudDB zone名称，打开Cloud DB zone实例
      const cloudDBZoneConfig = new cloudDb.CloudDBZoneConfig(ZONE_NAME);
      this.cloudDbZone = cloudDbInstance.openCloudDBZone(cloudDBZoneConfig);
    } catch (err) {
      logger.error("init CloudDBZoneWrapper error: " + err);
    }
  }

  async queryResource(userId, type, pageNum, pageSize) {
    let resList: ResourceResp = [];
    if (!this.cloudDbZone) {
      console.log("CloudDBClient is null, try re-initialize it");
      return;
    }
    try {
      let topics: topic[] = await this.queryTopic();
      resList = await this.queryResourceList(userId, type, pageNum, pageSize, topics);
    } catch (error) {
      this.logger.info("query resource error " + error);
    } finally {
      return resList;
    }
  }

  // async queryTopicResIds(userId, queryNum, type) {
  //   let resourceIds = [];
  //   // 先查询该userid关联了哪些topic
  //   const topicQuery = cloudDb.CloudDBZoneQuery.where(user_topic).equalTo('user_id', userId);
  //   const topicResp = await this.cloudDbZone.executeQuery(topicQuery);
  //   let topicData = topicResp.getSnapshotObjects();
  //   this.logger.info("query user success ");
  //   console.log('resp=>', topicData.length);
  //   let followedTopicIds = [];
  //   for (let i = 0; i < topicData.length; i++) {
  //     followedTopicIds.push(topicData[i].getTopic_id());
  //   }
  //   resourceIds = await this.queryFollowedTopicResourceIds(followedTopicIds, queryNum, type);
  //   return resourceIds;
  // }

  async queryFollowedTopicIds(userId) {
    // 先查询该userid关联了哪些topic
    const topicQuery = cloudDb.CloudDBZoneQuery.where(user_topic).equalTo('user_id', userId);
    const topicResp = await this.cloudDbZone.executeQuery(topicQuery);
    let topicData = topicResp.getSnapshotObjects();
    this.logger.info("query queryFollowedTopicIds success ");
    console.log('resp=>', topicData.length);
    let followedTopicIds = [];
    for (let i = 0; i < topicData.length; i++) {
      followedTopicIds.push(topicData[i].getTopic_id());
    }
    return followedTopicIds;
  }

  getResourceList(resourceData, topics) {
    let resList = [];
    for (let i = 0; i < resourceData.length; i++) {
      let dataQ: resource = resourceData[i];
      resList.push(new ResourceResp(
        dataQ.getId(),
        dataQ.getTitle(),
        dataQ.getBrief(),
        dataQ.getHeader_src(),
        dataQ.getType(),
        dataQ.getWeb_url(),
        dataQ.getPublish_date(),
        this.getTopicNames(topics, dataQ.getTopics()),
        dataQ.getViews_count(),
        dataQ.getCollect_count(),
        dataQ.getLikes_count(),
        dataQ.getTag(),
        dataQ.getBanner_src(),
        dataQ.getMedia_src(),
        null, // isLiked
        null, // isCollected
        null // isViewed
      ))
    }
    return resList;
  }

  async queryResourceList(userId, type, pageNum, pageSize, topics) {
    let resourceData = [];
    let listResp;
    let resList = [];
    let totalCount = 0;
    try {
      const resourceQuery = cloudDb.CloudDBZoneQuery.where(resource);
      resourceQuery.equalTo('type', type);
      if (userId) { // 如果用户传了userId 则查询用户关注主题下的资源数据
        const followedTopicIds = await this.queryFollowedTopicIds(userId);
        const resourceIds = await this.queryFollowedTopicResourceIds(followedTopicIds, pageNum, pageSize, type);
        totalCount = resourceIds.length;
        resourceQuery.in('id', resourceIds);
        resourceQuery.limit(pageSize, (pageNum - 1) * pageSize);
      } else { // 否在直接查询资源列表
        totalCount = await this.cloudDbZone.executeCountQuery(resourceQuery, 'id');
        resourceQuery.limit(pageSize, (pageNum - 1) * pageSize);
      }
      if (pageNum <= Math.ceil(totalCount / pageSize)) {
        const resourceResp = await this.cloudDbZone.executeQuery(resourceQuery);
        resourceData = resourceResp.getSnapshotObjects();
        this.logger.info("query queryFeedResource success ");
        console.log('resp=>', resourceData.length);
        console.log('totalCount=>', totalCount);
        if (resourceData.length > 0) {
          resList = this.getResourceList(resourceData, topics)
        }
      }
      listResp = new ListResp(
        pageNum,
        pageSize,
        Math.ceil(totalCount / pageSize),
        totalCount,
        totalCount > pageNum * pageSize,
        resList
      )
    } catch (error) {
      this.logger.info("query queryFeedResource error " + error);
    } finally {
      return listResp;
    }
  }

  async queryLikedIds(userid) {
    let likedIds = [];
    if (!this.cloudDbZone) {
      console.log("CloudDBClient is null, try re-initialize it");
      return;
    }
    try {
      const likeQuery = cloudDb.CloudDBZoneQuery.where(user_like);
      likeQuery.equalTo('user_id', userid);
      const likeResp = await this.cloudDbZone.executeQuery(likeQuery);
      let likeData = likeResp.getSnapshotObjects();
      this.logger.info("query like resource success ");
      console.log('resp like likeData=>', likeData.length);
      for (let index = 0; index < likeData.length; index++) {
        const element: user_like = likeData[index];
        likedIds.push(element.getResource_id());
      }
      return likedIds;
    } catch (error) {
      this.logger.info("query resource error " + error);
    }
  }

  async queryCollectedIds(userid) {
    let collectedIds = [];
    if (!this.cloudDbZone) {
      console.log("CloudDBClient is null, try re-initialize it");
      return;
    }
    try {
      const collectedQuery = cloudDb.CloudDBZoneQuery.where(collect);
      collectedQuery.equalTo('user_id', userid);
      const collectedResp = await this.cloudDbZone.executeQuery(collectedQuery);
      let collectedData = collectedResp.getSnapshotObjects();
      this.logger.info("query collected resource success ");
      console.log('resp collected=>', collectedResp.length);
      for (let index = 0; index < collectedData.length; index++) {
        const element: collect = collectedData[index];
        collectedIds.push(element.getResource_id());
      }
      return collectedIds;
    } catch (error) {
      this.logger.info("query resource error " + error);
    }
  }

  async queryHistoryIds(userid) {
    let historyIds = [];
    if (!this.cloudDbZone) {
      console.log("CloudDBClient is null, try re-initialize it");
      return;
    }
    try {
      const historyQuery = cloudDb.CloudDBZoneQuery.where(user_history);
      historyQuery.equalTo('user_id', userid);
      const historyResp = await this.cloudDbZone.executeQuery(historyQuery);
      let historyData = historyResp.getSnapshotObjects();
      this.logger.info("query historyData resource success ");
      console.log('resp historyData=>', historyData.length);
      for (let index = 0; index < historyData.length; index++) {
        const element: user_history = historyData[index];
        historyIds.push(element.getResource_id());
      }
      return historyIds;
    } catch (error) {
      this.logger.info("query resource error " + error);
    }
  }

  async queryTopic() {
    // 查询所有主题
    const cloudDBZoneQuery = cloudDb.CloudDBZoneQuery.where(topic);
    const resp = await this.cloudDbZone.executeQuery(cloudDBZoneQuery);
    let listData = resp.getSnapshotObjects();
    this.logger.info("query topic success ");
    console.log('queryTopic listData=>', listData.length);
    return listData;
  }

  getTopicNames(topics: topic[], tidStr: string) {
    let topicNames = []
    let topicIds: string[] = tidStr.split(',');
    for (let index = 0; index < topicIds.length; index++) {
      const tid = topicIds[index];
      let name: topic[] = topics.filter(tp => tp.getId() === tid);
      topicNames.push(name[0].getName())
    }
    return topicNames;
  }

  async queryFollowedTopicResourceIds(topicIds, pageNum, pageSize, type) {
    let resourceIds = [];
    try {
      const topicResourceQuery = cloudDb.CloudDBZoneQuery.where(topic_resource);
      if (topicIds.length > 0) {
        topicResourceQuery.in('topic_id', topicIds);
      }
      topicResourceQuery.equalTo('type', type);
      //topicResourceQuery.limit(pageSize, (pageNum - 1) * pageSize);
      const topicResourceResp = await this.cloudDbZone.executeQuery(topicResourceQuery);
      let topicResourceRespData = topicResourceResp.getSnapshotObjects();
      this.logger.info("query topicResourceRespData success ");
      console.log('resp topicResourceRespData=>', topicResourceRespData.length);
      for (let index = 0; index < topicResourceRespData.length; index++) {
        const element: topic_resource = topicResourceRespData[index];
        if (!resourceIds.includes(element.getResource_id())) {
          resourceIds.push(element.getResource_id());
        }
      }
    } catch (error) {
      this.logger.info("query resource error " + error);
    } finally {
      return resourceIds;
    }
  }

  async queryTotalCount(topicIds, type) {
    let totalCount = 0;
    try {
      const topicResourceQuery = cloudDb.CloudDBZoneQuery.where(topic_resource);
      topicResourceQuery.in('topic_id', topicIds);
      topicResourceQuery.equalTo('type', type);
      totalCount = await this.cloudDbZone.executeCountQuery(topicResourceQuery, 'resource_id');
      console.log('totalCount=>', totalCount);
    } catch (error) {
      this.logger.info("query resource error " + error);
    } finally {
      return totalCount;
    }
  }
}



